<!--
 * @Description: 销量排行模块
 * @Author: 梁萌
 * @Date: 2020-12-06 16:00:04
 * @LastEditTime: 2020-12-19 16:41:22
 * @FilePath: \vision\src\components\Rank.vue
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="rank_ref"></div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      startValue: 0, //区域缩放起点值
      endValue: 9, //区域缩放终点值
      timeID: null, //定时器标识
    };
  },
   created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("rankData", this.getData);
  },
  mounted() {
    this.initChart();
    //this.getData();
     //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "rankData",
      chartName: "rank",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    clearInterval(this.timeID);
    window.removeEventListener("resize", this.screenAdapter);
     //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("rankData");
  },
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.rank_ref,  this.theme); //初始化echarts实例对象
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎地区销售排行",
          left: 20,
          top: 20,
        },
        grid: {
          top: "40%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [{ type: "bar" }],
      };
      this.chartIntance.setOption(initOption);
      this.chartIntance.on("mouseover", () => {
        clearInterval(this.timeID);
      });
      this.chartIntance.on("mouseout", () => {
        this.startInterval();
      });
    },

    //获取服务器数据
    getData(ret) {
      //const { data: ret } = await this.$http.get("rank");
      this.allData = ret;
      //对数据进行从大到小排序
      this.allData.sort((a, b) => {
        return b.value - a.value;
      });
      this.updateChart();
      this.startInterval(); //启动定时器
    },

    //更新图表
    updateChart() {
      //省份名称
      const proviceNameArr = this.allData.map((item) => {
        return item.name;
      });

      //省份数据
      const proviceDataArr = this.allData.map((item) => {
        return item.value;
      });

      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"],
      ];

      const dataOption = {
        xAxis: {
          data: proviceNameArr,
        },
        dataZoom: {
          show: false, //隐藏缩放滚动条
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: proviceDataArr,
            itemStyle: {
              color: (arg) => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }

                return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: targetColorArr[0],
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1],
                  },
                ]);
              },
            },
          },
        ],
      };

      this.chartIntance.setOption(dataOption);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
      //和分辨率大小相关配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
            },
          },
        ],
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize();
    },
    //定时调整缩放区域的值
    startInterval() {
      if (this.timeID) {
        clearInterval(this.timeID);
      }
      this.timeID = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 2000);
    },
  },
   computed:{
    ...mapState(['theme'])
  },
  watch:{
    theme(){
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    }
  }
};
</script>

<style>
</style>